<template>
	<view class="content" @touchmove.stop.prevent>
		<headItem :showBack="true" :activeTitle="typeName" ref="refHeadItem" @getParentList="gethomedata"></headItem>
		<view class="nr-box">

			<view class="nr-item">
				<view class="nr-title"><span
						class="line-item"></span>{{type=="1" ? "报名列表" : type=="2" ? "诚意金列表" : type=="3" ? "到店列表": type=="4" ? "订单列表":"全部"}}
				</view>

				<view class="nr-sort">
					<view class="item" @click="hanldSrot">时间排序</view>
					<view class="item" @click="staffShow = true">{{staffName}}<u-icon name="arrow-down"></u-icon></view>
				</view>
			</view>

			<view class="nr-list m-b">
				<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y" @scrolltoupper="upper"
					@scrolltolower="lower" @scroll="scroll">

					<view class="nr-item" v-for="(item,index) in listData" :key="index">
						<view class="user-box">
							<view class="item-name no-wrap">{{item.real_name || ''}}</view>
							<view class="item-sf">{{item.staffInfo.real_name || ''}}<br>销售顾问</view>
						</view>
						<view class="cart-box">
							<view class="item-cart no-wrap" style="text-align: center;">
								{{item.cartInfo ? item.cartInfo.title : item.cart || ''}}
							</view>
							<view class="item-cart" style="text-align: center;">
								{{item.mobile || ''}}
							</view>
						</view>
						<view class="but-box">
							<view class="item-cart" v-if="type === 1">
								报名：{{item.apply_time | dateFormat}}
								{{item.apply_time | getWeek}}
							</view>
							<view class="item-cart" v-if="type === 2">
								支付：{{item.pay_time| dateFormat}}
								{{item.pay_time | getWeek}}
							</view>
							<view class="item-cart" v-if="type === 3">
								到店：{{item.arrival_date | dateFormat}}
								{{item.arrival_date | getWeek}}
							</view>
							<view class="item-cart" v-if="type === 4">
								订单：{{item.bargain_date | dateFormat}}
								{{item.bargain_date | getWeek}}
							</view>
							<view class="kh-box">
								<!-- <view class="item-kh bg-01"
									@click="getUserInfo(type === '1' || type === '2' ? item.id : item.user_id)">
									<image src="/static/images/v2/icon/index-kuxq.png"
										style="width: 22rpx;height: 22rpx;"></image>客户详情
								</view> -->
								<view class="item-kh bg-02" @click="callphone(item)">
									<image src="/static/images/v2/icon/phone.png" style="width: 22rpx;height: 22rpx;">
									</image>电话沟通
								</view>
								<!-- <view class="item-kh bg-02" @click="callphone2(item)" v-else>
									<image src="/static/images/v2/icon/phone.png" style="width: 22rpx;height: 22rpx;">
									</image>电话沟通
								</view> -->
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
		<!-- 添加拨打电话用户状态	 -->
		<u-popup :show="phoneShow" round="8" :closeOnClickOverlay="false" :safeAreaInsetTop='false' :closeable="true"
			mode="center" @close="close">
			<view class="client-box">
				<view class="client-title">
					客户跟进
				</view>
				<view class="client-cell">
					<u-cell :border='false' title="客户状态" :isLink="true" @click="userShow = true">
						<text slot="value" style="color: #3477FC;">{{summaryKey[form.summary]}}</text>
					</u-cell>
				</view>
				<view class="client-cell">
					<u-cell :border='false' title="是否同意加微信" :isLink="true" @click="wxShow = true">
						<text slot="value" style="color: #3477FC;">{{wxkey[form.wechat]}}</text>
					</u-cell>
				</view>

				<view class="client-nr">
					<view class="nr-title">跟进记录</view>
					<view class="nr-text">
						<u-textarea height="90" v-model="form.content" placeholder="请输入内容"></u-textarea>
					</view>
				</view>

				<view class="client-but">
					<u-button type="primary" text="提交" @click='formOK'></u-button>
				</view>

			</view>
			<u-toast ref="uToast"></u-toast>
			<!-- 客户状态		 -->
			<u-picker :show="userShow" @confirm="usertypeSelectOk" closeOnClickOverlay keyName="label"
				@close="UsercloseType" :columns="typeColumns2" @cancel="usertypeSelectNot"></u-picker>
			<!-- 是否加微信 -->
			<u-picker :show="wxShow" @confirm="wxtypeSelectOk" closeOnClickOverlay keyName="label" @close="wxclose"
				:columns="wxColumns" @cancel="wxtypeSelectNot"></u-picker>
		</u-popup>


		<!-- 类型弹出层 -->
		<u-picker :show="typeShow" @confirm="typeSelectOk" closeOnClickOverlay keyName="label" @close="typeclose"
			:columns="typeColumns" @cancel="typeSelectNot"></u-picker>
		<!-- 类型弹出层 -->
		<u-picker :show="show" @confirm="selectOk" closeOnClickOverlay keyName="title" @close="show = false"
			:columns="AcitveColumns" :immediateChange='true' @cancel="show = false" mode="selector"></u-picker>
		<u-picker :show="staffShow" @confirm="staffSelectOk" closeOnClickOverlay keyName="label" @close="staffclose"
			:columns="staffList" @cancel="staffSelectNot"></u-picker>

		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		getActiveList,
		gethomeData,
		getWorkListApi,
		getMakeList,
		getWorkcallApi,
		getLabel,
		getWorksaveApi
	} from '@/api/system/index'
	import {
		mapMutations,
		mapGetters
	} from "vuex";
	import headItem from "@/components/head-item/index.vue"
	import moment from 'moment'; //引入
	export default {
		components: {
			headItem
		},
		data() {
			return {
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				navbar: 0, //状态栏高度
				statusBar: 0, // 状态栏高度 + 导航栏高度  
				customBar: 0, // 自定义标题与胶囊对齐高度
				show: false,
				dateshow: false,
				minDate: '0',
				maxDate: '0',
				defaultDate: [],
				phoneShow: false,
				typeShow: false,
				//关键词
				keyword: '',
				typeName: "",
				typeid: '',
				AcitveColumns: [],
				loadingIs: true,
				listData: [],
				tabBarHeight: 0,
				safeareaHeight: 0,
				type: null,
				queryParam: {
					activeId: null,
					page: 1,
					limit: 10,
					typeSort: "desc"
				},
				typeColumns: [
					[{
							label: '全部',
							key: 'all'
						},
						{
							label: '待跟客户',
							key: 'wait'
						},
						{
							label: '报名',
							key: 'notSincerity'
						},
						{
							label: '诚意金',
							key: 'okSincerity'
						},
						{
							label: '订单',
							key: 'orderNum'
						},
						{
							label: '重点',
							key: 'important'
						},
						{
							label: '有效',
							key: 'notBuy'
						},
						{
							label: '未接',
							key: 'notAnswer'
						},
						{
							label: '无效',
							key: 'invalid'
						}
					]
				],
				typeColumns2: [
					[{
							label: '待跟客户',
							key: 'wait'
						},
						{
							label: '报名',
							key: 'notSincerity'
						},
						{
							label: '诚意金',
							key: 'okSincerity'
						},
						{
							label: '订单',
							key: 'orderNum'
						},
						{
							label: '重点',
							key: 'important'
						},
						{
							label: '有效',
							key: 'notBuy'
						},
						{
							label: '未接',
							key: 'notAnswer'
						},
						{
							label: '无效',
							key: 'invalid'
						}
					]
				],
				summaryKey: {
					all: "全部客户",
					wait: "待跟客户",
					notSincerity: "报名",
					okSincerity: "诚意金",
					orderNum: "订单",
					important: "重点",
					notBuy: "有效",
					notAnswer: "未接",
					invalid: "无效",
				},
				userShow: false,
				wxShow: false,
				typeName: '',
				wxkey: {
					0: "否",
					1: "是"
				},
				wxColumns: [
					[{
							label: '是',
							key: 1
						}, {
							label: '否',
							key: 0
						}

					]
				],
				form: {
					content: '',
					wechat: 0,
					summary: '',
					activeId: null,
					userId: null

				},
				staffList: [],
				staffShow: false,
				staffName: '销售顾问',
				iconList: [
					"../../static/images/v2/icon/rank-01.png",
					"../../static/images/v2/icon/rank-02.png",
					"../../static/images/v2/icon/rank-03.png"
				]
			}
		},
		filters: {
			dateFormat(val) {
				if (val) {
					val = typeof val === "number" ? val * 1000 : val
					val = moment(val).format('YYYY-MM-DD');
				}
				return val || '';
			},
			getWeek(date) { // 时间戳
				const week = moment(date).day();
				switch (week) {
					case 1:
						return '星期一';
					case 2:
						return '星期二';
					case 3:
						return '星期三';
					case 4:
						return '星期四';
					case 5:
						return '星期五';
					case 6:
						return '星期六';
					case 0:
						return '星期日';
				}
				return '';
			},

		},
		computed: {
			...mapGetters(["avatar", "nickname", "mobile", "position", 'autoname'])
		},
		mounted() {
			// if (app.globalData.safeareaHeight > 0) {
			// 	this.safeareaHeight = app.globalData.safeareaHeight;
			// }

		},
		onLoad(options) {
			if (options.type) {
				this.type = parseInt(options.type)
			}
			// this.getDateList();

		},
		created() {
			this.topHight = uni.getSystemInfoSync().statusBarHeight

		},
		onShow() {
			//触发子组件获取
			this.$refs.refHeadItem.getAcitveData()
			var stort_ac_id = uni.getStorageSync('active_id')
			var stort_ac_title = uni.getStorageSync('active_title')
			var stort_ac_key = uni.getStorageSync('summary');
			this.queryParam.activeId = stort_ac_id
			this.getLabelList()
			// this.getAcitveData()
			// this.gethomedata()
		},
		methods: {
			async getLabelList() {
				var params = {
					activeId: this.queryParam.activeId
				}
				await getLabel(params).then(res => {
					if (!res.code) {
						var staffList = res.data.staffList
						if (staffList) {
							var arr = Object.entries(staffList).map(([key, value]) => {

								return {
									id: key,
									label: value
								};
							});
							var allObj = {
								id: null,
								label: "全部"
							}
							arr.unshift(allObj)
							this.staffList = [arr];
						}
					}

				});
			},
			async getDateList() {

				console.log("我进来了", this.type)
				if (this.type === 1) {

					this.queryParam.applySummaryType = 1
					//报名和诚意金接口
					this.getWorkListData()
				} else if (this.type === 2) {
					this.queryParam.summary = "okSincerity"
					this.getWorkListData()
				} else {
					this.type === 3 ? this.queryParam.arrival_state = 1 : this.type === 4 ? this.queryParam
						.bargain_state = 1 : ""
					// console.log("请求参数", this.queryParam)
					//预约接口
					this.getWorkMakeListData()
				}
			},
			gethomedata(e) {
				this.queryParam.activeId = e.active_id
				this.listData = []
				this.getDateList()
			},
			//排序
			hanldSrot() {
				this.listData = []
				this.queryParam.typeSort = this.queryParam.typeSort == 'desc' ? "asc" : this.queryParam.typeSort == 'asc' ?
					"desc" : "desc";
				this.queryParam.page = 1
				this.getDateList()
			},
			async getWorkListData() {

				getWorkListApi(this.queryParam).then(res => {

					if (!res.code) {

						this.listData = [...this.listData, ...res.data]

						this.total = res.count
					}
					this.listshow = false

				})

			},
			close() {
				this.phoneShow = false
			},
			getUserInfo(id) {

				this.$tab.navigateTo(`/pages/work/detail?id=${id}`)
			},
			async getWorkMakeListData() {

				getMakeList(this.queryParam).then(res => {

					if (!res.code) {

						this.listData = [...this.listData, ...res.data]

						this.total = res.count
					}
					this.listshow = false

				})

			},
			async staffSelectOk(e) {
				var data = e.value[0]
				this.queryParam.staffId = data.id
				this.staffName = data.label
				this.queryParam.page = 1
				this.listData = []
				this.staffShow = false
				await this.getDateList()
			},
			staffclose() {
				this.staffShow = false
			},
			staffSelectNot() {
				this.staffShow = false
			},
			upper: function(e) {
				// console.log(e)
			},
			lower: function(e) {
				//判断当前数据数量是否
				if (this.listData.length >= this.total) return false
				this.queryParam.page++
				this.getDateList()
			},
			scroll: function(e) {
				// console.log(e)
				this.old.scrollTop = e.detail.scrollTop
			},
			goTop: function(e) {
				// 解决view层不同步的问题
				this.scrollTop = this.old.scrollTop
				this.$nextTick(function() {
					this.scrollTop = 0
				});
				// uni.showToast({
				// 	icon: "none",
				// 	title: "纵向滚动 scrollTop 值已被修改为 0"
				// })
			},
			Dateconfirm(e) {

				this.defaultDate = e

				console.log(this.defaultDate.length);

				this.dateshow = false
				this.gethomedata()

			},
			Dateclose() {

				this.dateshow = false
			},
			callphone(item) {
				uni.makePhoneCall({
					phoneNumber: item.mobile, //仅为示例
					success(res) {
						//用户拒绝权限后任然会走这个函数
					},
					fail(err) {},
					complete() {}
				});
				// var that = this
				// var arr = {
				// 	activeId: item.active_id,
				// 	userId: item.id,
				// 	mobile: item.mobile,
				// 	realName: item.real_name
				// }
				// that.form.wechat = item.wechat
				// that.form.summary = item.summary
				// that.form.userId = item.id
				// that.form.activeId = item.active_id
				// getWorkcallApi(arr).then(res => {

				// 	if (!res.code) {

				// 		uni.makePhoneCall({
				// 			phoneNumber: item.mobile, //仅为示例
				// 			success(res) {
				// 				//用户拒绝权限后任然会走这个函数
				// 			},
				// 			fail(err) {},
				// 			complete() {
				// 				that.phoneShow = true
				// 			}
				// 		});
				// 	}
				// })

			},
			callphone2(item) {

				var that = this

				var arr = {
					activeId: item.active_id,
					userId: item.id,
					mobile: item.mobile,
					realName: item.real_name
				}
				that.form.wechat = item.wechat
				that.form.summary = item.summary
				that.form.userId = item.id
				that.form.activeId = item.active_id
				getWorkcallApi(arr).then(res => {

					if (!res.code) {
						uni.makePhoneCall({
							phoneNumber: item.mobile, //仅为示例
							success(res) {
								//用户拒绝权限后任然会走这个函数，
							},
							fail(err) {},
							complete() {}
						});
					}

				})
			},

			chooseTimed() {


				let date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				if (month <= 9) {
					month = '0' + month;
				}
				if (day <= 9) {
					day = '0' + day;
				}

				let minyear = year - 1;
				this.minDate = minyear + '-' + month + '-' + day;
				let maxyear = year + 1;
				this.maxDate = maxyear + '-' + month + '-' + day;
				this.defaultDate[0] = year + '-' + month + '-' + day;
			},

			getSelectType() {
				this.show = true
			},
			change(e) {
				// console.log("e:", e);
			},
			selectOk(e) {

				this.listData = []
				var data = e.value[0]
				uni.setStorageSync('active_id', data.id);
				uni.setStorageSync('active_title', data.title);
				this.typeName = data.title
				this.typeid = data.id
				this.show = false
				this.gethomedata()

			},
			async formOK() {

				//判断当前
				if (this.form.summary == null || this.form.summary == '') {

					this.$refs.uToast.show({
						type: 'warning',
						message: "请选择客户状态"

					})
					return false

				}
				if (('0' != (this.form.wechat).toString()) && (this.form.wechat == null || this.form.wechat == '')) {

					this.$refs.uToast.show({
						type: 'warning',
						message: "请选择微信添加状态"

					})
					return false

				}
				if (this.form.content == null || this.form.content == '') {

					this.$refs.uToast.show({
						type: 'warning',
						message: "请填写跟进记录"

					})
					return false
				}
				var that = this

				await getWorksaveApi(that.form).then(res => {

					if (!res.code) {

						that.$refs.uToast.show({
							type: 'success',
							message: '添加成功',
							complete() {
								that.phoneShow = false
								that.form = {
									content: '',
									wechat: 0,
									summary: '',
									activeId: null,
									userId: null
								}
								that.listData = []
								that.getWorkListData()
							}

						})


					}


				})




			}

		},
	}
</script>

<style lang="scss">
	page {
		background-color: #f5f5f5FF !important;
		padding-bottom: constant(safe-area-inset-bottom);
		/* 兼容 iOS < 11.2 */
		padding-bottom: env(safe-area-inset-bottom);
		/* 兼容 iOS >= 11.2 */
	}

	.scroll-Y {
		// height: 100%;
		// flex: 1;

		// overflow-y: auto;
		height: calc(100vh - 660rpx);
		// padding-bottom: 100rpx;
	}

	.scroll-view_H {
		white-space: nowrap;
		width: 100%;
	}

	.time-box {
		display: flex;
		justify-content: space-between;
	}

	.time-size {
		font-size: 26rpx;
		color: #fff;
		align-self: center;
		padding: 0px 2px;
	}

	.content {
		.top-bg {
			position: absolute;
			width: 100%;
			height: 537rpx;
			// background: linear-gradient(0deg, #FFFFFF 0%, #3477FC 100%);
			background-image: url('');
			z-index: -100;
			background-size: cover;
		}


		.nr-box {

			width: 100%;
			margin: 0 auto;
			// z-index: 100;
			// height: 100%;

			padding: 20rpx 0rpx;

			.nr-item {
				width: 100%;
				padding: 10rpx 40rpx;
				background: #FFFFFF;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				margin-bottom: 1px;

				.line-item {
					width: 6rpx;
					padding: 1rpx 5rpx;
					background: #254EF8;
					border-radius: 436rpx 436rpx 436rpx 436rpx;
					margin-right: 15rpx;
				}

				.nr-title {
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 28rpx;
					color: rgba(0, 0, 0, 0.85);
					line-height: 48rpx;
					text-align: left;
					font-style: normal;
					text-transform: none;

				}

				.mb-10 {
					margin-bottom: 20rpx;
				}

				.nr-sort {
					width: 100%;
					display: flex;
					justify-content: flex-start;
					padding: 20rpx 25rpx;

					.item {
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						font-size: 26rpx;
						color: rgba(45, 52, 64, 0.85);
						line-height: 36rpx;
						text-align: left;
						font-style: normal;
						text-transform: none;
						margin-right: 40rpx;
						display: flex;
					}
				}

				.nr-div {
					width: 100%;
					display: flex;
					justify-content: space-between;
					// flex-wrap: wrap;
					// align-items: center;
					// ajju
					margin-bottom: 5px;
					padding: 25rpx;

					.item {
						text-align: center;

						.nr-num {
							font-family: PingFang SC, PingFang SC;
							font-weight: bold;
							font-size: 36rpx;
							color: #2553F7;
							line-height: 36rpx;
							text-align: left;
							font-style: normal;
							text-transform: none;
							padding-bottom: 20rpx;
						}

						.nr-item-tile {
							font-family: PingFang SC, PingFang SC;
							font-weight: 500;
							font-size: 26rpx;
							color: rgba(78, 89, 105, 0.85);
							line-height: 36rpx;
							text-align: left;
							font-style: normal;
							text-transform: none;
						}
					}


				}

			}


			.nr-list {
				width: 95%;
				padding: 20rpx;

				border-radius: 16rpx 16rpx 16rpx 16rpx;
				margin: 0 auto;
				margin-top: 40rpx;

				// margin-bottom: 50rpx;
				// background-color: yellow;
				// height: calc(100vh - 810rpx);
				.nr-item {
					width: 100%;
					display: flex;
					// justify-content: space-between;
					align-items: center;
					margin-bottom: 20rpx;
					padding: 25rpx 20rpx;
					background: #FFFFFF;

					.user-box {
						width: 20%;
					}

					.cart-box {
						width: 30%;

						// display: flex;
						// flex-wrap: wrap;
						// justify-content: center;
						text-align: center;
					}

					.but-box {
						width: 50%;

						.kh-box {
							display: flex;
							justify-content: flex-end;
							padding-top: 10px;

							.bg-01 {
								background: #254EF8;
							}

							.bg-02 {
								background: #17CF20FF;
							}

							.item-kh {

								border-radius: 4rpx 4rpx 4rpx 4rpx;
								padding: 10rpx 10rpx;
								color: #FFFFFFFF;
							}
						}


					}

					.item-name {
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						font-size: 30rpx;
						color: rgba(0, 0, 0, 0.85);
						line-height: 48rpx;
						text-align: left;
						font-style: normal;
						text-transform: none;
					}

					.item-sf {
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						font-size: 16rpx;
						color: #254EF8;
						line-height: 30rpx;
						text-align: left;
						font-style: normal;
						text-transform: none;
					}

					.item-cart {
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						font-size: 23rpx;
						color: rgba(0, 0, 0, 0.85);
						line-height: 48rpx;
						text-align: center;
						font-style: normal;
						text-transform: none;
					}

				}

				.list-bg {
					background-color: #f6faffFF !important
				}

			}

			.m-b {
				// height: calc(100vh - 100rpx);
				// overflow: hidden;
			}


		}

		.logo {
			height: 200rpx;
			width: 200rpx;
			margin-top: 200rpx;
			margin-left: auto;
			margin-right: auto;
			margin-bottom: 50rpx;
		}

		.text-area {
			display: flex;
			justify-content: center;
		}

		.title {
			font-size: 36rpx;
			color: #8f8f94;
		}
	}

	.no-wrap {
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.client-box {

		width: 650rpx;
		padding-bottom: 40rpx;
		margin-top: 50rpx;

		.client-title {
			color: #333333;
			font-size: 36rpx;
			font-weight: bold;
			padding: 0rpx 40rpx;
			padding-bottom: 20rpx;
		}

		.client-cell {
			width: 95%;
			margin: 0 auto;
			background-color: #fff;
			box-shadow: 0 0 10px #ccc;
			padding: 5rpx 0rpx;
			border-radius: 5px;
			margin-bottom: 10px;
		}

		.client-nr {
			width: 95%;
			margin: 0 auto;
			background-color: #fff;
			border-radius: 5px;

			.nr-title {
				padding: 20rpx;
				border-bottom: 1px solid #ccc;
			}

			.nr-text {
				padding: 10rpx 0rpx;
			}
		}

		.client-but {
			width: 95%;
			margin: 0 auto;
			padding-top: 90rpx;
		}
	}
</style>